package service

import (
	"context"
	__ "video-server/basic/proto"
	"video-server/handler/model"
)

// server is used to implement helloworld.GreeterServer.
type Server struct {
	__.UnimplementedVideoServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) Video(_ context.Context, in *__.VideoReq) (*__.VideoResp, error) {

	video := model.Video{
		Title: in.Title,
		Cover: in.Cover,
		Type:  in.Type,
	}

	if err := video.CreateVideo(); err != nil {
		return &__.VideoResp{
			Code: 400,
			Msg:  "视频创建失败",
		}, nil
	}

	return &__.VideoResp{
		Code: 200,
		Msg:  "视频添加成功",
	}, nil
}

func (s *Server) Drama(_ context.Context, in *__.DramaReq) (*__.DramaResp, error) {

	drama := model.Drama{
		VideoId:      int(in.VideoId),
		EpisodeId:    int(in.EpisodeId),
		Name:         in.Name,
		Type:         in.Type,
		Introduction: in.Introduction,
	}

	if err := drama.CreateDrama(); err != nil {
		return &__.DramaResp{
			Code: 400,
			Msg:  "电视剧添加失败",
		}, nil
	}

	return &__.DramaResp{
		Code: 200,
		Msg:  "电视剧添加成功",
	}, nil
}

func (s *Server) AuditRecord(_ context.Context, in *__.AuditRecordReq) (*__.AuditRecordResp, error) {

	record := model.AuditRecord{
		VideoId: int(in.VideoId),
		Status:  int(in.Status),
		Opinion: in.Opinion,
	}

	if err := record.CreateAuditRecord(); err != nil {
		return &__.AuditRecordResp{
			Code: 400,
			Msg:  "视频审核状态通知失败",
		}, nil
	}

	return &__.AuditRecordResp{
		Code: 200,
		Msg:  "视频审核状态通知成功",
	}, nil
}

func (s *Server) Permission(_ context.Context, in *__.PermissionReq) (*__.PermissionResp, error) {

	permission := model.Permission{
		UserId:  int(in.UserId),
		VlpTime: in.VlpTime,
	}

	if err := permission.CreatePermission(); err != nil {
		return &__.PermissionResp{
			Code: 400,
			Msg:  "VIP权限验证失败",
		}, nil
	}

	return &__.PermissionResp{
		Code: 200,
		Msg:  "VIP权限验证成功",
	}, nil
}
